package cn.tudo.egweibo2.controller;

import cn.tudo.egweibo2.mapper.CommentMapper;
import cn.tudo.egweibo2.pojo.dto.CommentDTO;
import cn.tudo.egweibo2.pojo.entity.Comment;
import cn.tudo.egweibo2.pojo.vo.CommentVO;
import cn.tudo.egweibo2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        if(userVO==null){
            return  2;
        }
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);
        return 1;

    }
    @GetMapping("selectByWeiboId")
    public List<CommentVO>selectByWeiboId(int id){
        return commentMapper.selectByWeiboId(id);
    }




}
